import 'package:flutter/cupertino.dart';
import 'package:full_display/generated/l10n.dart';
import 'package:full_display/unit/screen/screen_brightness.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:screen_brightness/screen_brightness.dart';

class MontageProvider extends ChangeNotifier {
//拼接所有设备
  List deviceList = [];

  static final switchStr = ['开', '关', '重启', '位置', '老化', '测灯'];
  static final switchImageStr = [
    'icon_on',
    'icon_off',
    'icon_restart',
    'icon_location',
    'icon_old',
    'icon_light'
  ];
  static final functionStr = ['添加拼接', '定时', '蓝牙', '拼接配置', '角度调节', '拼接计算器'];
  static final functionImageStr = [
    'icon_montage',
    'icon_timer',
    'icon_blue_tooth',
    'icon_config',
    'icon_rotate',
    'icon_math'
  ];

  final List drawerList = [
    {"title": "全开", "img": 'm_quankai'},
    {"title": "全关", "img": 'm_quanguan'},
    {"title": "重启", "img": 'm_chongqi'},
    {"title": "老化模式", "img": 'm_laohua'},
    {"title": "退出老化模式", "img": 'm_laohua'},
    {"title": "测灯模式", "img": 'm_ceshi'},
    {"title": "退出测灯", "img": 'm_tuichu'},
    {"title": "位置模式", "img": 'm_weizhi'},
  ];

  final List playModelList = [
    {"title": S.current.sequence_play, "isSelect": false},
    {"title": S.current.random_play, "isSelect": false},
    {"title": S.current.single_play, "isSelect": false},
  ];

  //Wi-Fi配置
  List wifiSetting = [
    {"title": "11112332323", "isSelect": false},
    {"title": "2asdas", "isSelect": false},
    {"title": "3asds", "isSelect": false},
    {"title": "4", "isSelect": false},
    {"title": "5", "isSelect": false},
    {"title": "6", "isSelect": false},
    {"title": "7", "isSelect": false},
    {"title": "8", "isSelect": false},
    {"title": "9", "isSelect": false},
    {"title": "10", "isSelect": false},
  ];
  //改变Wi-Fi配置的状态
  changeWIFISettingState(int index) {
    // wifiSetting.forEach((element) {
    //   element['isSelect'] = false;
    // });
    bool state = wifiSetting[index]['isSelect'];
    wifiSetting[index]['isSelect'] = !state;
    notifyListeners();
  }

  //蓝牙配置
  List bluetoothList = [
    {"title": "XLZ-6C21A2215E2B", "isSelect": false},
    {"title": "XLZ-6C21A2215E2B", "isSelect": false},
    {"title": "XLZ-6C21A2215E2B", "isSelect": false},
  ];
  //改变Wi-Fi配置的状态
  changeBluetoothSettingState(int index) {
    bluetoothList.forEach((element) {
      element['isSelect'] = false;
    });
    bluetoothList[index]['isSelect'] = true;
    notifyListeners();
  }

  //角度配置
  List angleOneList = [
    {"title": "704A0EED1256", "isSelect": true},
    {"title": "704A0EED1256", "isSelect": false},
    {"title": "704A0EED1256", "isSelect": false},
    {"title": "704A0EED1256", "isSelect": false},
    {"title": "704A0EED1256", "isSelect": false},
    {"title": "704A0EED1256", "isSelect": false},
    {"title": "704A0EED1256", "isSelect": false},
    {"title": "704A0EED1256", "isSelect": false},
  ];

  //改变角度配置的状态
  changeAnglehSettingState(int index) {
    angleOneList.forEach((element) {
      element['isSelect'] = false;
    });
    angleOneList[index]['isSelect'] = true;
    notifyListeners();
  }

  //定时¸
  List clockList = [
    {"startTime": "22:00", "endTime": "8:00"},
    {"startTime": "22:00", "endTime": "8:00"},
  ];
  deleteTime(int index) {
    clockList.removeAt(index);
  }

  //拼接
  List pinjieList = [
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
    {"name": "名称", "type": "型号S50", "layout": "3行5列", "model": "模式高"},
  ];
  deletePinjie(int index) {
    pinjieList.removeAt(index);
  }

  //拼接模式
  List pinjieModel = [
    {"title": "1-1是薄垫片", "img": "", "isSelect": true},
    {"title": "1-1是厚垫片", "img": "", "isSelect": false},
  ];

  //拼接管理
  List pinjieManagerBtn = [
    {"title": "拼接1", "isSelect": true},
    {"title": "拼接2", "isSelect": false},
    {"title": "拼接3", "isSelect": false},
  ];
  List pinjieManagertitle = [
    "1",
    "",
    "3",
    "",
    "",
    "",
    "4",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
    "",
  ];

// sn code
  List SNCodeList = [
    "703E9777EB88",
    "703E9777EB88",
    "703E9777EB88",
    "703E9777EB88",
    "703E9777EB88",
    "703E9777EB88",
    "703E9777EB88",
    "703E9777EB88",
  ];

  //play model
  // List mplayModel = [
  //   {'img': 'm_neiqie', 'text': '内切', "index": 0},
  //   {'img': 'm_waiqie', 'text': '外切', "index": 1},
  //   {'img': 'm_tongbu', 'text': '同步', "index": 2},
  // ];

  //同步数据
  List sycData = [
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
    "1231232313",
  ];

//改变角度配置的状态
  changePinjieManagerState(int index) {
    pinjieManagerBtn.forEach((element) {
      element['isSelect'] = false;
    });
    pinjieManagerBtn[index]['isSelect'] = true;
    notifyListeners();
  }

  //改变播放模式当前状态
  changePlayState(int index) {
    playModelList.forEach((element) {
      element['isSelect'] = false;
    });
    playModelList[index]['isSelect'] = true;
    notifyListeners();
  }

  String getStrByIndex(int index, List<String> str) {
    return str[index];
  }

  String getImageByIndex(int index, List<String> str) {
    return str[index];
  }

  ///desc:图片路径
  var imagePath = <String>[''];

  ///desc:清空图片
  ///params:无
  Future<void> cleanPath() async {
    imagePath.clear();
    imagePath.add('');
    notifyListeners();
  }

  ///desc:申请权限
  ///params:无
  Future<void> getPermission() async {
    final galleryStatus = await Permission.photos.status;
    if (galleryStatus.isGranted) {
      await getLocalImageList();
    } else {
      print('object拒绝');
    }
  }

  ///desc:获取本地图片
  ///params:
  Future<void> getLocalImageList() async {
    final picker = ImagePicker();
    final pickedFile =
        await picker.pickImage(source: ImageSource.gallery, imageQuality: 50);
    var path = pickedFile?.path ?? "";
    imagePath.add(path);
    var temp = <String>[];
    for (var element in imagePath) {
      if (element.isNotEmpty) {
        temp.add(element);
      }
    }
    imagePath.clear();
    if (temp.length < 4) {
      temp.add('');
    }
    imagePath.addAll(temp);
    notifyListeners();
  }

  ///desc:设置屏幕亮度
  ///params:亮度值0.0-1.0
  Future<void> setScreenBrightness(double value) async {
    ScreenBrightnessUtils.instance.setBrightness(value / 100);
    /*ScreenBrightnessUtils.instance.onBrightnessChange((event){
      print("object=>${event}");
    });*/
  }

  //设置每个设备的IP和端口使用sn当key
  Map deviceInfotoSn = {};
}
